package com.tarena.lbs.service.impl;

import com.tarena.lbs.mapper.ContentMapper;
import com.tarena.lbs.pojo.dto.ContentQuery;
import com.tarena.lbs.pojo.dto.ContentSaveParam;
import com.tarena.lbs.pojo.dto.ContentUpdateParam;
import com.tarena.lbs.pojo.entity.Content;
import com.tarena.lbs.pojo.vo.ContentVO;
import com.tarena.lbs.service.ContentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
@RequiredArgsConstructor
public class ContentServiceImpl implements ContentService {
    @Autowired
    ContentMapper contentMapper;
    @Override
    public void saveArticle(ContentSaveParam contentSaveParam) {
        log.debug("保存文章参数:{}",contentSaveParam);
        Content content = new Content();
        BeanUtils.copyProperties(contentSaveParam,content);
//        if(content.getId() == null)
            content.setCreateTime(new Date());
            contentMapper.insert(content);

    }

    @Override
    public List<ContentVO> selectcontent(ContentQuery contentQuery) {
        log.debug("查询文章参数:{}", contentQuery);
        List<ContentVO> contentList = contentMapper.selectContent(contentQuery);
        return contentList;

/*

        // 构建布尔查询
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        // 动态添加查询条件
        if (contentQuery.getId() != null) {
            boolQuery.must(QueryBuilders.termQuery("id", contentQuery.getId()));
        }

        if (contentQuery.getCategoryName() != null) {
            boolQuery.must(QueryBuilders.termQuery("categoryName", contentQuery.getCategoryName()));
        }

        if (contentQuery.getCategoryStatus() != null) {
            boolQuery.must(QueryBuilders.termQuery("categoryStatus", contentQuery.getCategoryStatus()));
        }

        // 创建查询
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQuery)
                .build();

        SearchHits<ContentVO> searchHits = elasticsearchRestTemplate.search(
                searchQuery,
                ContentVO.class,
                IndexCoordinates.of("content")
        );

        // 转换结果
        return searchHits.stream()
                .map(hit -> hit.getContent())
                .collect(Collectors.toList());*/
    }

    @Override
    public void updateContent(ContentUpdateParam contentUpdateParam) {
        if (contentUpdateParam == null || contentUpdateParam.getId() == null) {
            log.error("更新文章参数为空");
            throw new IllegalArgumentException("更新参数不能为空");
        }
        log.debug("更新文章参数:{}",contentUpdateParam);
        Content content = contentMapper.selectById(contentUpdateParam.getId());
        if (content == null) {
            log.error("文章不存在, ID: {}", contentUpdateParam.getId());
            throw new RuntimeException("文章不存在");
        }
        BeanUtils.copyProperties(contentUpdateParam,content);
        // 修正：先设置更新时间，再执行更新
        content.setUpdateTime(new Date());
        contentMapper.update(content);

        log.info("文章更新成功, ID: {}", content.getId());
    }

    @Override
    public void delete(Integer id) {
        log.debug("删除文章参数：{}", id);
        contentMapper.deleteById(id);
    }



    }